package com.ylkj.patient.utils;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.text.TextUtils;

import com.baidu.mobstat.StatService;
import com.examination.mlib.constants.UserInfoConstants;
import com.example.messagemodule.MessageHelper;
import com.huawei.agconnect.common.network.AccessNetworkManager;
import com.huawei.agconnect.crash.AGConnectCrash;
import com.snow.common.crash.CrashHandler;
import com.uuzuche.lib_zxing.activity.ZXingLibrary;
import com.yilijk.base.network.https.HttpUtils;
import com.yilijk.base.utils.DevicesUtils;
import com.yilijk.base.utils.SharedPreferencesUtils;
import com.yljk.live.common.utils.MediaCountyManager;
import com.ylkj.patient.BuildConfig;
import com.ylkj.patient.MyActivityManager;


/**
 * 第三方SDK初始化
 */
public class ThreeSDKInitUtils {
    private static ThreeSDKInitUtils utils;
    private boolean isInitialized = false;

    private ThreeSDKInitUtils() {
    }

    public static ThreeSDKInitUtils getInstance() {
        if (utils == null) {
            synchronized (ThreeSDKInitUtils.class) {
                if (utils == null) {
                    utils = new ThreeSDKInitUtils();
                }
            }
        }
        return utils;
    }

    public void init(Application application) {

        try {
            initHuaweiCrashInfo();
        } catch (Throwable throwable) {

        }
        setIsInitialized(true);
        registerActivityListener(application);
//        百度统计初始化
        initBaiDu(application);
        moduleInit(application);
        //推送
        PushUtils.getPushUtils().initRomDevice(application);
        ZXingLibrary.initDisplayOpinion(application);
        if (BuildConfig.OPEN_CRASH) {
            CrashHandler.getInstance().init(application, "/Android/data/com.ylkj.patient/files/");
        }

        MediaCountyManager.getInstance().launchInit(application);
    }


    public boolean isIsInitialized() {
        return isInitialized;
    }

    public void setIsInitialized(boolean isInitialized) {
        this.isInitialized = isInitialized;
    }

    /**
     * 华为crash统计 键值对信息
     */
    private void initHuaweiCrashInfo() {
        AccessNetworkManager.getInstance().setAccessNetwork(true);

        AGConnectCrash.getInstance().setCustomKey("dominNo", HttpUtils.getUrl());
        AGConnectCrash.getInstance().setCustomKey("channel", com.yilijk.base.utils.DevicesUtils.getChannel());
        AGConnectCrash.getInstance().setCustomKey("deviceBrand", DevicesUtils.getDeviceBrand());
        AGConnectCrash.getInstance().setCustomKey("deviceId", DevicesUtils.getDeviceId());
        AGConnectCrash.getInstance().setCustomKey("deviceName", DevicesUtils.getDeviceName());
//        AGConnectCrash.getInstance().log(Log.WARN, "set warn log.");
        String mobile = SharedPreferencesUtils.getInstance().getString(UserInfoConstants.account_mobile, null);
        if (!TextUtils.isEmpty(mobile) && mobile.length() > 4) {
            String shortMobile = mobile.substring(mobile.length() - 4);
            AGConnectCrash.getInstance().setUserId(HttpUtils.getUrl() + "*" + DevicesUtils.getDeviceBrand() + "*" + DevicesUtils.getDeviceId() + "*" + shortMobile);
        } else {
            AGConnectCrash.getInstance().setUserId(HttpUtils.getUrl() + "*" + DevicesUtils.getDeviceBrand() + "*" + DevicesUtils.getDeviceId() + "*");
        }

    }

    private void registerActivityListener(Application application) {
        application.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onActivityStarted(Activity activity) {
                // TODO Auto-generated method stub
//                    appCount++;
            }

            @Override
            public void onActivityResumed(Activity activity) {
                // TODO Auto-generated method stub
                MyActivityManager.getInstance().setCurrentActivity(activity);
            }

            @Override
            public void onActivityPaused(Activity activity) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onActivityStopped(Activity activity) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
                // TODO Auto-generated method stub
//                    appCount--;
//                    if (appCount == 0) {
//                        Toast.makeText(getApplicationContext(), "慧医天下APP已切入后台，请勿输入账号信息！", Toast.LENGTH_SHORT).show();
//                    }
//                    if (!isAppOnForeground(activity)) {
//                        Toast.makeText(getApplicationContext(), "慧医天下APP已切入后台", Toast.LENGTH_SHORT).show();
//                    }
                ViewInterceptUtil.check(activity);
            }

            @Override
            public void onActivityDestroyed(Activity activity) {
                // TODO Auto-generated method stub
            }
        });
    }


    private void initBaiDu(Application application) {
        // 通过该接口可以控制敏感数据采集，true表示可以采集，false表示不可以采集，
        // 该方法一定要最优先调用，请在StatService.start(this)之前调用，采集这些数据可以帮助App运营人员更好的监控App的使用情况，
        // 建议有用户隐私策略弹窗的App，用户未同意前设置false,同意之后设置true
        String token = SharedPreferencesUtils.getInstance().getString(UserInfoConstants.token, null);
        String mobile = SharedPreferencesUtils.getInstance().getString(UserInfoConstants.account_mobile, null);
        if (!TextUtils.isEmpty(token) && !TextUtils.isEmpty(mobile)) {
            StatService.setAuthorizedState(application, true);
        }else {
            StatService.setAuthorizedState(application, false);
        }
    }


    private void moduleInit(Application application) {
        MessageHelper.getMessageHelper().init(application, BuildConfig.OPEN_LOG_OUT);
    }

}
